用於接收從 Spring Boot 返回的支出數據類別。當發出請求期望獲得關於支出的資訊時,Spring Boot 會將數據以 JSON 格式返回,而可以使用 ExpensesResponse 類別來將對應的 JSON 數據轉換為 Java 物件。
public class ExpensesResponse {
@SerializedName("status")
private String status;
@SerializedName("message")
private String message;
@SerializedName("expensesDataList")
private List<ExpensesData> expensesDataList;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<ExpensesData> getExpensesDataList() {
return expensesDataList;
}
public void setExpensesDataList(List<ExpensesData> expensesDataList) {
this.expensesDataList = expensesDataList;
}
}
用於表示每筆支出數據的詳細資訊。ExpensesResponse 物件中的expensesDataList 屬性包含多個 ExpensesData 物件,每個物件表示一筆支出記錄,可以使用 get 方法來訪問這些屬性。
public class ExpensesData {
@SerializedName("id")
private Long id;
@SerializedName("expensesType")
private String expensesType;
@SerializedName("expensesName")
private String expensesName;
@SerializedName("expensesValue")
private int expensesValue;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getExpensesName() {
return expensesName;
}
public void setExpensesName(String expensesName) {
this.expensesName = expensesName;
}
public int getExpensesValue() {
return expensesValue;
}
public void setExpensesValue(int expensesValue) {
this.expensesValue = expensesValue;
}
public String getExpensesType() {
return expensesType;
}
public void setExpensesType(String expensesType) {
this.expensesType = expensesType;
}
}
用於定義你的 Android 和 Spring Boot 伺服器之間的 API 請求方法。這些方法都對應了不同的 API 端點:
public interface ApiService {
@GET("user/expenses")
Observable<ExpensesResponse> getExpensesData(@Query("date") String date);
@GET("user/expenses/all")
Observable<ExpensesResponse> getAllExpensesData();
@DELETE("user/expenses")
Observable<StatusResponse> deleteExpensesData(@Query("id") Long id);
}
@POST("user/expenses")
Observable<StatusResponse> insertExpenses(@Body ExpensesRequest expensesRequest);
用於管理 Retrofit 實例並提供一個全局的 ApiService 實例,以便在整個應用程式中使用 Retrofit 來發送請求。
public class RetrofitManager {
private static RetrofitManager mInstance = new RetrofitManager();
private ApiService apiService;
private RetrofitManager() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://請更改IP:8080/api/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
apiService = retrofit.create(ApiService.class);
}
public static RetrofitManager getInstance() {
return mInstance;
}
public ApiService getAPI() {
return apiService;
}
}
這邊以讀取方法為示範:
@Override
public void loadExpensesData(String date) {
apiService.getExpensesData(date)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<ExpensesResponse>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ExpensesResponse expensesResponse) {
//取得支出相關資料,並進行相關處理
presenter.showExpenses(expensesResponse.getExpensesDataList());
}
@Override
public void onError(Throwable e) {
Log.e("loadExpensesData", e.toString());
}
@Override
public void onComplete() {
}
});
}